﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace TwoWheels.Account
{
    public partial class Cart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
          
            if (!Page.IsPostBack)
            {
                
                //CartView.Columns.Clear();
                /*
                const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
                TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);
                Dictionary<int, int> product = (Session["UserShoppingCartInfo"] as UserShoppingCart).Products;

                /*
                DataTable tbl = new DataTable();
                tbl.Columns.Add(new DataColumn("Product Name"));
                tbl.Columns.Add(new DataColumn("Price"));
                tbl.Columns.Add(new DataColumn("Quantity"));   
                */
                //int i = 0;             
                /*

                List<tempInfo> gridList = new List<tempInfo>();
                foreach (int productID in product.Keys)
                {
                    tempInfo temp = new tempInfo();
                    var pr = from p in tdb.products where p.ProduktID == productID select new { p.NazwaProduktu, p.CenaJednostkowa };
                    temp.Name = (from n in pr select n.NazwaProduktu).ToList().First().ToString();
                    temp.Price = (from n in pr select n.CenaJednostkowa).ToList().First().ToString();
                    temp.Quantity = product[productID].ToString();
                    gridList.Add(temp);


                    objSource.InsertParameters.Add("Name", temp.Name);
                    objSource.InsertParameters.Add("Price", temp.Price);
                    objSource.InsertParameters.Add("Quantity", temp.Quantity);
                    i++;
                    //DataRow dr = tbl.DefaultView.Table.NewRow();
                    //dr["Product Name"] = (from n in pr select n.NazwaProduktu).ToList().First().ToString();
                    //dr["Price"] = (from n in pr select n.CenaJednostkowa).ToList().First().ToString(); //dodac znizki, precyzja
                    //dr["Quantity"] = product[productID];

                    //tbl.Rows.InsertAt(dr, i++);
                }
                //CartView.DataSource = gridList;
                //CartView.DataBind();
                */
                int i = 0;

                const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
                TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);
                Dictionary<int, int> product = (Session["UserShoppingCartInfo"] as UserShoppingCart).Products;
                foreach (int productID in product.Keys)
                {
                    i++;
                }

                if (i > 0) makeOrderButton.Visible = true;
                else emptyCartLabel.Visible = true;
            }

  
        }


        protected void deleteButton(Object sender, EventArgs e)
        {
            const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
            TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);
            Dictionary<int, int> product = (Session["UserShoppingCartInfo"] as UserShoppingCart).Products;

            Button button = sender as Button;

            int rowNumber = int.Parse(button.CommandArgument);
            //String ProductName = ((Label) CartView.DataKeys[rowNumber].Values[0]).Text;// DataKeys[rowNumber].Values[0];
            String ProductName = ((Label)CartView.Rows[rowNumber].FindControl("NameLabel")).Text;

            int searchedId = 0;
            foreach (int productID in product.Keys)
            {
                var pr = from p in tdb.products where p.ProduktID == productID select new { p.NazwaProduktu, p.CenaJednostkowa };
                String tempName = (from n in pr select n.NazwaProduktu).ToList().First().ToString();
                if (tempName.CompareTo(ProductName) == 0)
                {
                    searchedId = productID;
                    break;
                }
            }

            product.Remove(searchedId);
            Response.Redirect("~/Account/Cart.aspx");
        }

        protected void decButton(Object sender, EventArgs e)
        {
            const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
            TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);
            Dictionary<int, int> product = (Session["UserShoppingCartInfo"] as UserShoppingCart).Products;

            Button button = sender as Button;

            int rowNumber = int.Parse(button.CommandArgument);
            String ProductName = ((Label)CartView.Rows[rowNumber].FindControl("NameLabel")).Text;

            int searchedId = 0;
            foreach (int productID in product.Keys)
            {
                var pr = from p in tdb.products where p.ProduktID == productID select new { p.NazwaProduktu, p.CenaJednostkowa };
                String tempName = (from n in pr select n.NazwaProduktu).First().ToString();
                if (tempName.CompareTo(ProductName) == 0)
                {
                    searchedId = productID;
                    break;
                }
            }

            int howMany = product[searchedId];
            howMany--;
            if (howMany == 0)
            {
                product.Remove(searchedId);
            }
            else if(howMany > 0)
            {
                product[searchedId] = howMany;
            }
            Response.Redirect("~/Account/Cart.aspx");
        }


        public LinkedList<tempInfo> getCartData()
        {
            LinkedList<tempInfo> dataList = new LinkedList<tempInfo>();

            const string cnStr = @"Data Source=DUO-P8600;Initial Catalog=TwoWheels;Integrated Security=True";
            TwoWheelsDatabase tdb = new TwoWheelsDatabase(cnStr);
            if (Session["UserShoppingCartInfo"] == null)
                return null;
            
            Dictionary<int, int> product = (Session["UserShoppingCartInfo"] as UserShoppingCart).Products;
            foreach (int productID in product.Keys)
            {
                tempInfo temp = new tempInfo();
                var pr = from p in tdb.products where p.ProduktID == productID select new { p.NazwaProduktu, p.CenaJednostkowa };
                temp.Name = (from n in pr select n.NazwaProduktu).ToList().First().ToString();
                temp.Price = (from n in pr select n.CenaJednostkowa).ToList().First().ToString();
                temp.Quantity = product[productID].ToString();
                dataList.AddLast(temp);
            }

            return dataList;
        }

        protected void makeOrderButton_Click(object sender, EventArgs e)
        {
            (Session["UserShoppingCartInfo"] as UserShoppingCart).makeOrder();
            Response.Redirect("~/Order/Confirmation.aspx");
        }

        protected void objSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
        {

        }
     
    }
}